package com.nlx.notes.module.service.impl.charts;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.nlx.notes.module.bean.dto.querydb.FlowDayQueryDbDTO;
import com.nlx.notes.module.bean.dto.querydb.FlowMonthQueryDbDTO;
import com.nlx.notes.module.bean.vo.charts.ChartsDataVO;
import com.nlx.notes.module.core.util.AmountConverterUtils;
import com.nlx.notes.module.core.util.DateUtils;
import com.nlx.notes.module.entity.FlowDayStatistics;
import com.nlx.notes.module.entity.FlowMonthStatistics;
import com.nlx.notes.module.service.IFlowDayStatisticsService;
import com.nlx.notes.module.service.IFlowMonthStatisticsService;
import com.nlx.notes.module.service.charts.FlowChartsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class FlowChartsServiceImpl implements FlowChartsService {

   final IFlowDayStatisticsService flowDayStatisticsService;

   final IFlowMonthStatisticsService flowMonthStatisticsService;


    @Override
    public List<ChartsDataVO> dayCharts(YearMonth month,Long bookId, Long userId) {
        LocalDate monthDate = LocalDate.now();
        if(null != month){
            monthDate = DateUtils.parseMonthDate(month);
        }
        //获取当月第一天
        String firstDay = DateUtils.formatFirstDayFortMonth(monthDate);
        //获取当月最后一天
        String lastDay = DateUtils.formatLastDayForMonth(monthDate);
        FlowDayQueryDbDTO flowDayQueryDbDTO = new FlowDayQueryDbDTO();
        flowDayQueryDbDTO.setUserId(userId);
        flowDayQueryDbDTO.setStartDate(firstDay);
        flowDayQueryDbDTO.setEndDate(lastDay);
        flowDayQueryDbDTO.setBookId(bookId);
        List<FlowDayStatistics> flowDayStatisticsList = flowDayStatisticsService.findListByCondition(flowDayQueryDbDTO);
        if(CollUtil.isEmpty(flowDayStatisticsList)){
            return Collections.emptyList();
        }
        List<ChartsDataVO> chartsDataVOList = new ArrayList<>();
        ChartsDataVO chartsDataVO;
        for (FlowDayStatistics flowDayStatistics : flowDayStatisticsList) {
            chartsDataVO = new ChartsDataVO();
            chartsDataVO.setDate(flowDayStatistics.getDayTime());
            chartsDataVO.setExpense(AmountConverterUtils.convertFenToYuan(flowDayStatistics.getExpense()));
            chartsDataVO.setIncome(AmountConverterUtils.convertFenToYuan(flowDayStatistics.getIncome()));
            chartsDataVOList.add(chartsDataVO);
        }
        return chartsDataVOList;
    }

    @Override
    public List<ChartsDataVO> monthCharts(String year,Long bookId, Long userId) {
        if(StrUtil.isEmpty(year)){
            year = DateUtils.formatCurrentYear();
        }
        FlowMonthQueryDbDTO flowMonthQueryDbDTO = new FlowMonthQueryDbDTO();
        flowMonthQueryDbDTO.setUserId(userId);
        flowMonthQueryDbDTO.setYear(year);
        flowMonthQueryDbDTO.setBookId(bookId);
        List<FlowMonthStatistics> monthStatistics = flowMonthStatisticsService.findByCondition(flowMonthQueryDbDTO);
        if(CollUtil.isEmpty(monthStatistics)){
            return Collections.emptyList();
        }
        List<ChartsDataVO> chartsDataVOList = new ArrayList<>();
        ChartsDataVO chartsDataVO;
        for (FlowMonthStatistics flowDayStatistics : monthStatistics) {
            chartsDataVO = new ChartsDataVO();
            chartsDataVO.setDate(flowDayStatistics.getMonthTime());
            chartsDataVO.setExpense(AmountConverterUtils.convertFenToYuan(flowDayStatistics.getExpense()));
            chartsDataVO.setIncome(AmountConverterUtils.convertFenToYuan(flowDayStatistics.getIncome()));
            chartsDataVOList.add(chartsDataVO);
        }
        return chartsDataVOList;
    }
}
